/**
 * 防抖
 * @param fn
 * @param delay
 * @returns {(function(): void)|*}
 */
export const debounce = (fn, delay) => {
  let timer = null
  return function () {
    clearTimeout(timer)
    timer = setTimeout(() => {
      fn.apply(this, arguments)
    }, delay)
  }
}
/**
 * 节流
 * @param fn
 * @param delay
 * @returns {(function(): void)|*}
 */
export const throttle = (fn, delay) => {
  let lastTime = 0
  return function () {
    const now = Date.now()
    if(now - lastTime >= delay) {
      fn.apply(this, arguments)
      lastTime = now
    }
  }
}

/**
 * 数组对象去重
 * @param listObj
 * @returns {any[]}
 */
export const removeDuplicates = (listObj, key = "id") => {
  const uniqueMap = new Map()
  listObj.forEach(el => uniqueMap.set(el[key], el))
  return Array.from(uniqueMap.values())
}